/******************************************************************************
//             INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 2000-2001 Intel Corporation. All Rights Reserved.
//
//  Description:
//      Intel(R) JPEG library for X platform 
//
********************************************************************************/
#include "_ijx.h"

/******************************************************************************
// Name:            ijHuffmanEncode_Gray_DC
// Description:     Encoding scan data (DC) for Progressive mode
// Input Arguments:
//              ah: Indicates approximation high
//              al: Indicates approximation low
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pEncodeState: Identifies IJENCODESTATE strcuture.
// Output Arguments:
//        ppMCUBuf: Identifies MCU coefficients buffer.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_INSUFFICIENTBUF
//                  IJX_RTCODE_ERRPARSE
//                  
******************************************************************************/
IJXRESULT ijHuffmanEncode_Gray_DC(const Ipp16s **ppMCUBuf, 
                                   int ah,
                                   int al,
                                   IJBITTREAMSTATE *pStream,
                                   IJENCODESTATE *pEncodeState)
{
    IJXRESULT rtCode;
    if (0==ah) {
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(ppMCUBuf[0],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[0]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[0]);
    } else {
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(ppMCUBuf[0],
                                                     pStream->pBitStream,  
                                                     &(pStream->nUsedBufBitsLen),
                                                     al);

    }
    
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

//    pEncodeState->counter.nScanMCUCounter++;

    if ( (0<pEncodeState->nRstInterval) ){
        pEncodeState->counter.nDRICounter--;
        if (0==pEncodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalFwd(pStream,pEncodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijHuffmanEncode_444_DC
// Description:     Encoding scan data (DC) for Progressive mode
// Input Arguments:
//              ah: Indicates approximation high
//              al: Indicates approximation low
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pEncodeState: Identifies IJENCODESTATE strcuture.
// Output Arguments:
//        ppMCUBuf: Identifies MCU coefficients buffer.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_INSUFFICIENTBUF
//                  IJX_RTCODE_ERRPARSE
//                     
******************************************************************************/
IJXRESULT ijHuffmanEncode_444_DC(const Ipp16s **ppMCUBuf,
                                  int ah,
                                  int al,
                                  IJBITTREAMSTATE *pStream,
                                  IJENCODESTATE *pEncodeState)
{
    IJXRESULT rtCode;
    if (0==ah) {
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(ppMCUBuf[0],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[0]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[0]);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(ppMCUBuf[1],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[1]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[1]);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(ppMCUBuf[2],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[2]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[2]);
    } else {
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(ppMCUBuf[0],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(ppMCUBuf[1],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(ppMCUBuf[2],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
    }
    
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

//    pEncodeState->counter.nScanMCUCounter++;

    if ( (0<pEncodeState->nRstInterval) ){
        pEncodeState->counter.nDRICounter--;
        if (0==pEncodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalFwd(pStream,pEncodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijHuffmanEncode_422_DC
// Description:     Encoding scan data (DC) for Progressive mode
// Input Arguments:
//              ah: Indicates approximation high
//              al: Indicates approximation low
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pEncodeState: Identifies IJENCODESTATE strcuture.
// Output Arguments:
//        ppMCUBuf: Identifies MCU coefficients buffer.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_INSUFFICIENTBUF
//                  IJX_RTCODE_ERRPARSE
//                  
******************************************************************************/
IJXRESULT ijHuffmanEncode_422_DC( const Ipp16s **ppMCUBuf, 
                                  int ah,
                                  int al,
                                  IJBITTREAMSTATE *pStream,
                                  IJENCODESTATE *pEncodeState)
{
    IJXRESULT rtCode;
    if (0==ah) {
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(&(ppMCUBuf[0][0]),
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[0]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[0]);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(&(ppMCUBuf[0][64]),
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[0]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[0]);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(ppMCUBuf[1],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[1]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[1]);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(ppMCUBuf[2],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[2]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[2]);
    } else {
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(&(ppMCUBuf[0][0]),
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(&(ppMCUBuf[0][64]),
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(ppMCUBuf[1],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(ppMCUBuf[2],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
    }
    
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

//    pEncodeState->counter.nScanMCUCounter++;

    if ( (0<pEncodeState->nRstInterval) ){
        pEncodeState->counter.nDRICounter--;
        if (0==pEncodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalFwd(pStream,pEncodeState);
        }
    }

    return IJX_RTCODE_ERROR;
}

/******************************************************************************
// Name:            ijHuffmanEncode_411_DC
// Description:     Encoding scan data (DC) for Progressive mode
// Input Arguments:
//              ah: Indicates approximation high
//              al: Indicates approximation low
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pEncodeState: Identifies IJENCODESTATE strcuture.
// Output Arguments:
//        ppMCUBuf: Identifies MCU coefficients buffer.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_INSUFFICIENTBUF
//                  IJX_RTCODE_ERRPARSE
//                  
//                  
******************************************************************************/
IJXRESULT ijHuffmanEncode_411_DC( const Ipp16s **ppMCUBuf, 
                                  int ah,
                                  int al,
                                  IJBITTREAMSTATE *pStream,
                                  IJENCODESTATE *pEncodeState)
{
    IJXRESULT rtCode;
    if (0==ah) {
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(&(ppMCUBuf[0][0]),
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[0]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[0]);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(&(ppMCUBuf[0][64]),
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[0]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[0]);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(&(ppMCUBuf[0][128]),
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[0]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[0]);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(&(ppMCUBuf[0][192]),
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[0]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[0]);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(ppMCUBuf[1],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[1]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[1]);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCFirst_16s1u(ppMCUBuf[2],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(pEncodeState->dcPred[2]),
                                                    al,
                                                    pEncodeState->paIdxDCHuffTable[2]);
    } else {
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(&(ppMCUBuf[0][0]),
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(&(ppMCUBuf[0][64]),
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(&(ppMCUBuf[0][128]),
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(&(ppMCUBuf[0][192]),
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(ppMCUBuf[1],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
            
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        
        rtCode=ippiEncodeHuffman_Direct_JPEG_DCRefine_16s1u(ppMCUBuf[2],
                                                    pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    al);
    }
    
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

//    pEncodeState->counter.nScanMCUCounter++;

    if ( (0<pEncodeState->nRstInterval) ){
        pEncodeState->counter.nDRICounter--;
        if (0==pEncodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalFwd(pStream,pEncodeState);
        }
    }

    return IJX_RTCODE_ERROR;
}

/* EOF */
